<?php
/**
 *
 * User/IndexAction.class.php (上传图片处理类)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class UploadAction extends BaseAction
{



	public function index()
	{
		$typeArr = array("jpg", "png", "gif");//允许上传文件格式
		$path = "uploads/";//上传路径

		if (isset($_POST)) {
			$name = $_FILES['file']['name'];
			$size = $_FILES['file']['size'];
			$name_tmp = $_FILES['file']['tmp_name'];

			if (empty($name)) {
				echo json_encode(array("error"=>"您还未选择图片"));
				exit;
			}
			$type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型

			if (!in_array($type, $typeArr)) {
				echo json_encode(array("error"=>"清上传jpg,png或gif类型的图片！"));
				exit;
			}
			if ($size > (10000 * 1024)) {
				echo json_encode(array("error"=>"图片大小已超过10MB！"));
				exit;
			}
			$dir_name = empty($_GET['dir']) ? 'temporary' : trim($_GET['dir']);
			if ($dir_name !== '') {
				$path .= $dir_name . "/";
				//$save_url .= $dir_name . "/";
				if (!file_exists($path)) {
					mkdir($path);
				}
			}
			$ymd = date("Ymd");
			$path .= $ymd . "/";

			if (!file_exists( $path)) {
				mkdir($path);
			}

			$pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称
			$pic_url = $path . $pic_name;//上传后图片路径+名称
			if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹

				echo json_encode(array("error"=>"0","pic"=>$pic_url,"name"=>$pic_name));
			} else {
				echo json_encode(array("error"=>"上传有误，清检查服务器配置！"));
			}
		}
	}

	public function avarat(){
		$maxSize  = 1024*10240 ;//1M 设置附件上传大小
		$allowExts  = array("gif","jpg","jpeg","png");// 设置附件上传类型

		//include_once("UploadFile.class.php");
		import ( "@.Oauth.avarat.UploadFile" );
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = $maxSize;
		$upload->allowExts = $allowExts;
		$upload->savePath =  './Uploads/temporary/';// 设置附件
		$upload->saveRule = time().sprintf('%04s',mt_rand(0,1000));
		if(!$upload->upload()) {// 上传错误提示错误信息
			$errormsg = $upload->getErrorMsg();
			$arr =  array(
					'msg'=>'1111',
					'error'=>$errormsg, //返回错误
					'imgurl'=>'',//返回图片名
			);
			echo json_encode($arr);
			exit;

		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$imgurl = $info[0]['savename'];
		}
		$arr =  array(
				'msg'=>'0000',
				'error'=>'', //返回错误
				'imgurl'=>$imgurl,//返回图片名
		);
		echo json_encode($arr);
		exit;

	}
	public function avarat_img(){
		$img=$_REQUEST['img'];
		//echo ($img);exit;
		if($img==""){
			echo "图片没找到！";
			exit;
		}
		$img='./Uploads/temporary/'.$img;
		$this->assign('img',$img);

		$callback="show_head";//artdilog callback
		$this->assign('callback',$callback);
		$this->display();


	}

	public function avarat_submit(){
		$pic_name=$_REQUEST['pic_name'];
		$x=$_REQUEST['x'];
		$y=$_REQUEST['Y'];
		$w=$_REQUEST['w'];
		$h=$_REQUEST['h'];
		$targ_w = $targ_h = 100;
		//include_once("jcrop_image.class.php");
		import ( "@.Oauth.avarat.jcrop_image" );
		$filep="./Uploads/temporary/";
		$crop=new jcrop_image($filep, $pic_name,$x,$y,$w,$h,$targ_w,$targ_h);
		$file=$crop->crop();


	}



}
?>